package com.voxel.sense.infomsg.module.firm;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.utils.SizeUtils;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.widget.CommonImagePagerActivity;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.constants.CommonConstants;
import cyy.example.com.facotry.model.api.response.FirmListEntity;

/**
 * Created by chenyiyao on 2017/11/1.
 */

public class FirmListInfoAdapter extends RecyclerAdapter<FirmListEntity> {


    @Override
    protected int getItemViewType(int position, FirmListEntity companyDataBean) {
        return R.layout.item_recycle_list_firm;
    }

    public FirmListInfoAdapter(AdapterListener listener) {
        super(listener);
    }

    public FirmListInfoAdapter(List list, AdapterListener listener) {
        super(list, listener);
    }

    @Override
    protected ViewHolder onCreateViewHolder(View root, int viewType) {
        return new Viewholder(root);
    }

    static class Viewholder extends RecyclerAdapter.ViewHolder<FirmListEntity> {
        ImageView iv_pic;//图片信息
        TextView iv_level_icon;//监管登记
        TextView iv_firm_type_icon;//监管级别
        TextView tv_name;//公司名称
        TextView tv_category;//行业类别
        TextView tv_area;//地址
        TextView tv_street;//街道
        TextView tv_over_numb;//超标次数
        TextView tv_ill_numb;//违规次数
        TextView tv_fault_numb;//故障次数

        private final Context mContext;


        private Viewholder(View itemView) {
            super(itemView);
            mContext = itemView.getContext();
            iv_pic = itemView.findViewById(R.id.iv_pic);//图片信息
            iv_level_icon = itemView.findViewById(R.id.iv_level_icon);//监管登记
            tv_name = itemView.findViewById(R.id.tv_name);//公司名称
            iv_firm_type_icon = itemView.findViewById(R.id.iv_firm_type_icon);//监管级别
            tv_category = itemView.findViewById(R.id.tv_category);//行业类别
            tv_area = itemView.findViewById(R.id.tv_area);//区/
            tv_street = itemView.findViewById(R.id.tv_street);//街道
            tv_over_numb = itemView.findViewById(R.id.tv_over_numb);
            tv_ill_numb = itemView.findViewById(R.id.tv_ill_numb);
            tv_fault_numb = itemView.findViewById(R.id.tv_fault_numb);


        }

        @Override
        protected void onBind(final FirmListEntity bean) {
            //由于区地址右边有padding，当没有数据时隐藏，以免影响到其它布局的位置
            refreshData(bean);
            picClickEvent(bean);
        }

        private void picClickEvent(final FirmListEntity bean) {
            iv_pic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayList list = new ArrayList<>();
                    List<FirmListEntity.CompanyFileVoBean> companyFileVo = bean.getCompanyFileVo();
                    //判断是否有公司照片，没有则显示的是默认图
                    if (companyFileVo != null && companyFileVo.size() > 0
                            && !TextUtils.isEmpty(companyFileVo.get(0).getFilePath())) {
                        list.add(CommonConstants.FILE_FIRM_URL + companyFileVo.get(0).getFilePath());
                    } else {
                        list.add(R.mipmap.firm_logo_icon);
                    }
//                    CommonImagePagerActivity.showImages(mContext, list, 0);
                }
            });
        }

        private void refreshData(FirmListEntity bean) {
            FirmListEntity.CompanyBaseinfoVoBean baseInfo = bean.getCompanyBaseinfoVo();
            tv_name.setText("" + baseInfo.getCompanyName());
            tv_street.setText(baseInfo.getDetailAddr());
            tv_area.setText(baseInfo.getStreet());
            if (!TextUtils.isEmpty(baseInfo.getStreet())) {
                tv_area.setPadding(0, 0, SizeUtils.dp2px(10), 0);
            } else {
                tv_area.setPadding(0, 0, 0, 0);
            }
            tv_category.setText(baseInfo.getBusinessType());
            if (bean.getCompanyFileVo() != null && bean.getCompanyFileVo().size() > 0) {
                FirmListEntity.CompanyFileVoBean url = bean.getCompanyFileVo().get(0);
                GlideUtils.GlideLoadFirmPic(mContext, url.getFilePath(), R.mipmap.firm_logo_icon, iv_pic);
            } else {
                GlideUtils.GlideLoadPicNoDefault(mContext, R.mipmap.firm_logo_icon, iv_pic);
            }

            //管理等级
            if (!TextUtils.isEmpty(baseInfo.getRegulationLevel())) {
                iv_level_icon.setVisibility(View.VISIBLE);
//                GlideUtils.GlideLoadConfigPNGIcon(mContext, baseInfo.getRegulationLevel(), iv_level_icon);
                iv_level_icon.setText(baseInfo.getRegulationLevel());
//                switch (baseInfo.getRegulationLevel()){
//                    case "1":
//                        GlideUtils.GlideFitCenterLoadPic(mContext, R.mipmap.ic_level_a, iv_level_icon);
//                        break;
//                    case "2":
//                        GlideUtils.GlideFitCenterLoadPic(mContext, R.mipmap.ic_level_b, iv_level_icon);
//                        break;
//                    case "3":
//                        GlideUtils.GlideFitCenterLoadPic(mContext, R.mipmap.ic_level_c, iv_level_icon);
//                        break;
//                    default:
//                        break;
//                }
            } else {
                iv_level_icon.setVisibility(View.INVISIBLE);
//                GlideUtils.GlideFitCenterLoadPic(mContext, R.mipmap.icon_a, iv_level_icon);
            }
            //监管性质
            if (!TextUtils.isEmpty(baseInfo.getRegulationType())) {
//                GlideUtils.GlideLoadConfigPNGIcon(mContext, baseInfo.getRegulationType(), iv_firm_type_icon);
//                GlideUtils.GlideFitCenterLoadPic(mContext, R.mipmap.icon_a, iv_firm_type_icon);
                iv_firm_type_icon.setVisibility(View.VISIBLE);
                iv_firm_type_icon.setText(baseInfo.getRegulationType());
            } else {
                iv_firm_type_icon.setVisibility(View.INVISIBLE);
//                GlideUtils.GlideFitCenterLoadPic(mContext, R.mipmap.country, iv_firm_type_icon);
            }
            //是否有违规
            if (!TextUtils.isEmpty(baseInfo.getBadDescription())) {
                tv_ill_numb.setText(baseInfo.getBadDescription());
            }
            //是否有超标
            if (!TextUtils.isEmpty(bean.getAbnormalCount())) {
                tv_over_numb.setText(bean.getAbnormalCount());
            }

            //是否有设备故障
            if (!TextUtils.isEmpty(baseInfo.getOvershootDescription())) {
                tv_fault_numb.setText(baseInfo.getEquipmentfailure());
            }
        }
    }
}
